package com.veselinov.dictspeach.search.ui;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.ImageButton;
import android.widget.Toast;

import com.veselinov.dictspeach.R;
import com.veselinov.dictspeach.search.web.HttpCall;

public class MainActivity extends Activity {
	private static final String TAG = "vesselinov";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		final ImageButton button = (ImageButton) findViewById(R.id.imageButton1);
		button.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				// Perform action on click
				Log.v(TAG, "vesselinov: Should call server here");
				new CallServer()
						.execute("http://www.webservicex.net/CurrencyConvertor.asmx/"
								+ "ConversionRate?FromCurrency=EUR&ToCurrency=USD");
				// http://glosbe.com/gapi/translate?
				// from=pol&dest=eng&format=json&phrase=witaj&pretty=true
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	public class CallServer extends AsyncTask<String, Integer, String> {

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
		}

		@Override
		protected void onProgressUpdate(Integer... values) {
			// TODO Auto-generated method stub
			super.onProgressUpdate(values);
		}

		@Override
		protected String doInBackground(String... params) {
			return new HttpCall().DownloadText(params[0]);
		}

		@Override
		protected void onPostExecute(String result) {
			// ActivityManager am = (ActivityManager) cont
			// .getSystemService(Context.ACTIVITY_SERVICE);
			// ComponentName cn = am.getRunningTasks(1).get(0).topActivity;
			Toast t = Toast.makeText(getBaseContext(), result,
					Toast.LENGTH_LONG);
			t.setDuration(20000);
			t.show();
			Log.d("DownloadTextTask", result);
		}

	}

}
